package com.gz.mylife.app.keys;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.gson.Gson;
import com.gz.mylife.R;
import com.gz.mylife.app.BaseActivity;
import com.gz.mylife.http.AsyncHttpClient;
import com.gz.mylife.http.AsyncHttpResponseHandler;
import com.gz.mylife.http.RequestParams;
import com.gz.mylife.util.DateTimePickDialogUtil;
import com.gz.mylife.util.PrefUtils;
import com.zl.smartmall.library.po.TransData;
import com.zl.smartmall.library.util.Constants;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

public class TransferActivity extends BaseActivity{

	private EditText startDateTime;
	private EditText endDateTime;

	private String initStartDateTime = getCurrentTime(); // 初始化开始时间
	private String initEndDateTime = getCurrentTime(); // 初始化结束时间

	private String div_sn;
	private String target_phone;
	private String source_phone;
	private String begin_time;
	private String end_time;

	private EditText etPhoneNum1;
	private EditText etPhoneNum2;
	private EditText etPhoneNum3;
	private EditText etPhoneNum4;
	private EditText etPhoneNum5;
	private ArrayList<String> divList;
	private String url;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_transfer);
		setActionbarTitle("转授权", false);
		etPhoneNum1 = (EditText) findViewById(R.id.et_phone_num1);
		etPhoneNum2 = (EditText) findViewById(R.id.et_phone_num2);
		etPhoneNum3 = (EditText) findViewById(R.id.et_phone_num3);
		etPhoneNum4 = (EditText) findViewById(R.id.et_phone_num4);
		etPhoneNum5 = (EditText) findViewById(R.id.et_phone_num5);
		initData();

		// 两个输入框
		startDateTime = (EditText) findViewById(R.id.inputDate);
		endDateTime = (EditText) findViewById(R.id.inputDate2);

		startDateTime.setText(initStartDateTime);
		endDateTime.setText(initEndDateTime);

		startDateTime.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {

				DateTimePickDialogUtil dateTimePicKDialog = new DateTimePickDialogUtil(
						TransferActivity.this, initEndDateTime);
				dateTimePicKDialog.dateTimePicKDialog(startDateTime);

			}
		});

		endDateTime.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				DateTimePickDialogUtil dateTimePicKDialog = new DateTimePickDialogUtil(
						TransferActivity.this, initEndDateTime);
				dateTimePicKDialog.dateTimePicKDialog(endDateTime);
			}
		});
	}

	private void initData() {

		source_phone = PrefUtils.getStrPrefData(this, "phone_num", "");
		divList = getIntent().getStringArrayListExtra("divList");
		url = Constants.RECORD_URL;

	}

	private String getCurrentTime() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		String date = format.format(new Date());
		return date;
	}

	public void back(View v){
		finish();
	}

	//转授权
	public void transfer(View v){
		begin_time = startDateTime.getText().toString().trim();
		end_time = endDateTime.getText().toString().trim();
		if (begin_time.length()<0) {
			Toast.makeText(TransferActivity.this, "请选择起始时间", 0).show();
			return;
		}
		if (end_time.length()<0) {
			Toast.makeText(TransferActivity.this, "请选择结束时间", 0).show();
			return;
		}
		System.out.println("begin_time==="+begin_time);
		System.out.println("end_time==="+end_time);
		if (divList.size()>0) {
			System.out.println("-----------------div开始");
			div_sn = divList.get(0);
			StringBuffer sb = new StringBuffer(div_sn);
			for (int i = 1; i < divList.size(); i++) {
				sb.append(","+divList.get(i));
			}
			div_sn = sb.toString();
			System.out.println("div_sn===="+div_sn);
			String phone1 = etPhoneNum1.getText().toString().trim();
			String phone2 = etPhoneNum2.getText().toString().trim();
			String phone3 = etPhoneNum3.getText().toString().trim();
			String phone4 = etPhoneNum4.getText().toString().trim();
			String phone5 = etPhoneNum5.getText().toString().trim();

			List<String> phone = new ArrayList<String>();
			if (!TextUtils.isEmpty(phone1)) {
				String target_phone = phone1;
				phone.add(target_phone);
			}
			if (!TextUtils.isEmpty(phone2)) {
				String target_phone = phone2;
				phone.add(target_phone);
			}
			if (!TextUtils.isEmpty(phone3)) {
				String target_phone = phone3;
				phone.add(target_phone);
			}
			if (!TextUtils.isEmpty(phone4)) {
				String target_phone = phone4;
				phone.add(target_phone);
			}
			if (!TextUtils.isEmpty(phone5)) {
				String target_phone = phone5;
				phone.add(target_phone);
			}


			String phone_num;
			if (phone.size()>0) {
				System.out.println("-----------------phone开始");
				phone_num = phone.get(0);
				StringBuffer sbf = new StringBuffer(phone_num);
				for (int i = 1; i < phone.size(); i++) {
					sbf.append(","+phone.get(i));
				}
				phone_num = sbf.toString();
				target_phone = phone_num;
			}else {

				Toast.makeText(this, "请至少输入一个手机号！", 0).show();
			}
			AsyncHttpClient httpClient=new AsyncHttpClient();
			RequestParams params=new RequestParams();
			params.put("source_phone", source_phone);
			params.put("target_phone", target_phone);
			params.put("div_sn", div_sn);
			params.put("begin_time", begin_time);
			params.put("end_time", end_time);
			Toast.makeText(this, "正在授权...", 0).show();
			System.out.println(url);
			httpClient.get(url, params, new AsyncHttpResponseHandler(){

				@Override
				public void onSuccess(String content) {
					super.onSuccess(content);

					Gson gson = new Gson();
					System.out.println("content===="+content);
					TransData mTransData = gson.fromJson(content, TransData.class);
					if (mTransData.ref==0) {
						Toast.makeText(TransferActivity.this, mTransData.msg, 0).show();
						//PrefUtils.removeStrPrefData(getApplicationContext(), Constants.KEYS_URL);
						finish();
					}
					if (mTransData.ref==1) {
						Toast.makeText(TransferActivity.this, mTransData.err+"授权失败", 0).show();
					}
				}

				@Override
				public void onFailure(Throwable error, String content) {
					super.onFailure(error, content);

					Toast.makeText(TransferActivity.this, "请求失败", 0).show();

				}
			});

		}else {
			Toast.makeText(this, "未选择钥匙", 0).show();
		}

	}

}
